Embedded Networking and Communications

For this week the assignment is to design, build and connect wired or wireless node(s) with network or bus addresses and a local interface. This is something extremely handy with the Interface and Application Programing week and the Output week. With these two taken into account, the needed connection between both will be the embedded Networking.

Introduction to Serial Communication

When we talk about serial communication we are talking about a way that the microcontroller is communication via a cable with another device, in this case the computer. To do so, we need to first define the speed rate at which the devices will be communication, for example, 9600 bps (bits per second).

Then, we need to start serial communication by setting a baud rate in the setup() function:

Serial.begin(9600); // Starts serial communication at 9600 bps

After that, we can send a message, we can do so from the Microcontroller to our computer using Serial.print() or Serial.println()

Serial.println("Hello, world!"); // Sends "Hello, world!" to the computer

With all of this in mind, we will be able to send messages and most important, commands to the Arduino to send information and It will be able to send back said information.

Previous Assignment Code Demonstration

I will use the code provided on last week’s Assignment to demonstrate what We’ve done previously:

#include 

#define STEP_PIN 1
#define DIR_PIN 2

void setup() {
    Serial.begin(9600); // This is where we begin the Serial communication
    pinMode(STEP_PIN, OUTPUT);
    pinMode(DIR_PIN, OUTPUT);
}

void loop() {
    if (Serial.available() > 0) {
        char command = Serial.read();
        switch (command) {
            case 'L':
                Serial.println("Command received: Move motor left");
                digitalWrite(DIR_PIN, LOW);
                stepMotor(2000);  // Move motor for 2 seconds at the time
                break;
            case 'R':
                Serial.println("Command received: Move motor right");
                digitalWrite(DIR_PIN, HIGH);
                stepMotor(2000);  // Move motor for 2 seconds at the time
                break;
            default:
                Serial.println("Unknown command received");
                break;
        }
    }
}

void stepMotor(int duration) {
    unsigned long startTime = millis();
    while (millis() - startTime < duration) {
        digitalWrite(STEP_PIN, HIGH);
        delayMicroseconds(500);
        digitalWrite(STEP_PIN, LOW);
        delayMicroseconds(500);
    }
}

void toggleLED(int pin) {
    digitalWrite(pin, !digitalRead(pin));
}

And as we interpretate this code, the Microcontroller will receive the ‘R’ or ‘L’, but it will send back a message that confirms that our code was successfully received. This is a way to put up a flag and make sure that the microcontroller is doing it’s job.

Adding Another Layer: The Interface

This week will add another layer that is the interface and how can we connect it with our microcontroller.

The microcontroller will be connected directly to the Python program, therefore, the communication needs to be established on the Python interface.

Python Code for Communication

To do so, we can take an part of the code and see how it will work:

import tkinter as tk
from tkinter import filedialog
import cv2
from PIL import Image, ImageTk
import serial

# Initialize serial communication with the Xiao Seed RP2040
try:
    ser = serial.Serial('COM5', 9600)  # Depends on the COM port the MU is connected to
except serial.SerialException as e:
    print(f"Error: Could not open serial port 'COM5': {e}")
    ser = None

In python, we need to import a serial library and initialize the communication in the same way we did with Arduino IDE, although the language may change, the essence is the same. the ser variable is set at the 9600 bps and is using the COM5 Port. If the COM is not valid we will have a error message printed on the screen.

Communication in the Interface

And for the communication in the interface to move the motor Left or Right will be really similar to last week.

if ser:#If the serial is active
        if direction == 'left':#If left button is pressed
            ser.write(b'L')  # Send command to move motor left (in binary)
        elif direction == 'right':
            ser.write(b'R')  # Send command to move motor right (in binary)

With this communication in the python interface and the Arduino IDE code from top previously uploaded to the microcontroller, we can have a flawless communication for the final project as you can see in this abstract from the final project video: